
package com.productmodule.dao;
import com.productmodule.bean.TbInterface;

import java.sql.*;
import java.util.*;
import util.*;
public class TbInterfaceDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbInterface tbInterface=null;
    private List<TbInterface> tbInterfaceList=null;

    public boolean add(TbInterface inTbInterface){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_Interface(InterfaceCode,InterfaceName,InterfactMemo,userId) values(?,?,?,?)";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbInterface.getInterfaceCode());
        pt.setString(2,inTbInterface.getInterfaceName());
        pt.setString(3,inTbInterface.getInterfactMemo());
        pt.setInt(4, inTbInterface.getUserId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public boolean del(int inInterfaceId){
        return UtilDAO.del("Tb_Interface", "InterfaceId", inInterfaceId);
    }
    public boolean edit(TbInterface inTbInterface){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_Interface set InterfaceCode=?,InterfaceName=?,InterfactMemo=?,UserId=? where InterfaceId=?";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbInterface.getInterfaceCode());
        pt.setString(2,inTbInterface.getInterfaceName());
        pt.setString(3,inTbInterface.getInterfactMemo());
        pt.setInt(4, inTbInterface.getUserId());
        pt.setInt(5,inTbInterface.getInterfaceId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public TbInterface getTbInterfaceByInterfaceId(int inInterfaceId) {
        tbInterface=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Interface where InterfaceId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inInterfaceId);
            rs = pt.executeQuery();
            while (rs.next()) {
               tbInterface = getTbInterface(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbInterface;
    }
    public List<TbInterface> getTbInterfaceAll() {
        tbInterfaceList=new ArrayList<TbInterface>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Interface";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbInterfaceList.add(getTbInterface(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbInterfaceList;
    }
    public List<TbInterface> getTbInterfaceListByInterfaceId(String inInterfaceId) {
        tbInterfaceList=new ArrayList<TbInterface>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Interface where InterfaceId in("+inInterfaceId+")";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbInterfaceList.add(getTbInterface(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbInterfaceList;
    }
    public List<TbInterface> getTbInterfaceAll(VskPage vp) {
        tbInterfaceList=new ArrayList<TbInterface>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Interface";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_Interface", "InterfaceId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbInterfaceList.add(getTbInterface(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbInterfaceList;
    }
    public TbInterface getTbInterface(ResultSet rs) throws Exception{
        TbInterface tbInterface = new TbInterface();
        tbInterface.setInterfaceId(rs.getInt("InterfaceId"));
        tbInterface.setInterfaceCode(rs.getString("InterfaceCode"));
        tbInterface.setInterfaceName(rs.getString("InterfaceName"));
        tbInterface.setUserId(rs.getInt("UserId"));
        tbInterface.setInterfactMemo(rs.getString("InterfactMemo"));
        tbInterface.setCreateTime(rs.getString("CreateTime"));
        return tbInterface;
    }
}
